<?php
/**
 * Created by PhpStorm.
 * User: wangkxin@foxmail.com
 * Date: 2022/7/1
 * Time: 17:38
 */

namespace app\index\controller;
use Yansongda\Pay\Pay;
use Yansongda\Pay\Contract\HttpClientInterface;

class PayController
{
    protected $config = [
        'alipay' => [
            'default' => [
                'app_id' => '2021000118634530',
                // 应用私钥
                'app_secret_cert' => 'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCxfxfKJ+HD967vZS+ACbbd5g7/8WjiIkLTB6LKV83raH5jKcevUCeDagpatuJSVkXgoyaFFKRx30SE1XUyqZw7C8VqdYoUu6zr31jqnITl/11ranJQchP6l/Y8dMoVzOMer3xUcClVrRO4R37l5rYS/ogxexM2r+UdefmxPSqyN8ygYcoqAwPX2Y0GUk8sxSg+cdXDRXceLelPRztF9GDMagbzbvhrCMbFROGlbwjjNn6UWc30qFQ1yxV10PewB4r1BAEMHbCZnClpnKhoB3j/gxgElmv+RWoLBf3KDdugJM34PMkCpfkj5d2YGej8xy2zh6swAYme3U81pLj2p0jJAgMBAAECggEAe31M89zjehAsqWngrlgT3jZdYTx4GDx1+ZRqchS8s3ez1xIOV66QjyGLqqlqtJZJb4yTFCvUbVVEupY4P9M5WutJolO8icg4+rIdjrzuyYXtXPpmJgn4VHS9YDpxDtHJgmPdQHH4kB8mhSNY6mKlcnmcD7DtITMnWGawjdtchTib915wQZ4KfkCRd3PQ/oI+EG7ttx1WB+mAucEviGKxrJbS1CRLsbpsuBEJryPNgGdTz6msJ4pe2vxgQ8trRi6+/4Zchj1pzXToOqqru0oIATY7JPSS2rciPdfsVMNWOBmQlGe4F1sMPJCtttiYlZ69kZHNiy8f8T+Au1l4QMjoAQKBgQDi7jrEcwkB0jU4FZZ9HeLC7VxDlyNtYSSW6VoJQYhsg0hbWq7aIUxlE8I4vbHT0OAfI+rVH220/G32bwLQ/kcVbLX7023bfnHwZKCRISSJywkVJjj06xG9TIzk9Jk4fqiRniP1ZvkA6d+kgHy9gA93IJAZLW+GKVtXS1M9ZdvsCQKBgQDIO8MheQBz58zkiZOM9sTFMqXf7vCfryjwdmZqwpWf+2D15Nfswaw4ckIsN4B/2yF11enRov+cboEDcnLi+WG/IysTRt6XCcojMUz+eN5KHqKATT4MHGvFD4jfcxGJCaLQobyWnfiiH9sANWFju3gpSdMUtfCFq0aIfA8fBokmwQKBgBqzSwjYPCQRtp9NsvIvtQGYafWt78Yr2uFCvYw/QKz72WSeS9pwdouiAoUolKbGechQMo4273KPDOUk9KDgej6kFtl1bASaYQ+W12mcC6+Gyx/I6oSur9ylCCLcQLbr+CuB/tsd7YhU/DJfx82IJsmudCXyOaAAd7XSsA2WUYT5AoGAXxLxTh10N5Si1wj0BITPXYr08+UyX7/9wl+Vta96+CGWImtsk0BKk+q8vN4Fp6/WrWrKwP2SPSbDH23iWx9qaCU8uMR3wGOnlRo9or6lXbA6HI2QnpOIIGOF7ANpGhZ3NnT1ZNbRTbmudOTjv0UugvxIu5bAA9gVf4zYaTTjPoECgYAN1i2qo5vF2MeknEcuzVgQXBEY7Plbi609FOb1j1N5tw3xcF1+mEx14kpS60x+zna2t6JuW9zaRhzTO18Abfbjrx01eilwsDrenr7bLJMShXWn5b1DdtnMjjt5j4xQh3iyzMdmMhXmAwmbm0toXmYwHqYn5xDGExGlNPsO1fLKzg==',
                // 应用公钥证书路径
                'app_public_cert_path' =>  __DIR__ .'/cert/appCertPublicKey_2021000118634530.crt',
                // 支付宝公钥证书路径
                'alipay_public_cert_path' =>  __DIR__ .'/cert/alipayCertPublicKey_RSA2.crt',
                // 支付宝根证书路径
                'alipay_root_cert_path' =>  __DIR__ .'/cert/alipayRootCert.crt',
                // 'notify_url' => 'https://yansongda.cn/notify.html',
                // 'return_url' => 'https://yansongda.cn/return.html',
                'notify_url' => 'https://h5.huiqingoo.com/notify.html',
                'return_url' => 'https://h5.huiqingoo.com/return.html',
                'mode' => Pay::MODE_SANDBOX, // optional,设置此参数，将进入沙箱模式
            ],
        ],
        'logger' => [
            'enable' => false,
            'file' => './logs/alipay.log',
            'level' => 'info', // 建议生产环境等级调整为 info，开发环境为 debug
            'type' => 'single', // optional, 可选 daily.
            'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
        ],
        'http' => [ // optional
            'timeout' => 5.0,
            'connect_timeout' => 5.0,
            // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
        ]
    ];

    public function test()
    {
        return 'test';
    }

    public function web()
    {
        $result = Pay::alipay($this->config)->web([
            'out_trade_no' => '1'.time(),
            'total_amount' => '0.05',
            'subject' => 'yansongda 测试 - 1',
        ]);

        return $result;
    }


    /**
     *扫码支付未实现
     * 报错信息
     * code = "40004";
     * msg = "Business Failed";
     * sub_code = "ACQ.SECONDARY_MERCHANT_ID_BLANK"
     * sub_msg = "受理商户号不能为空"
     */
    public function scan()
    {
        pay::config($this->config);
        $result = Pay::alipay()->scan([
            'out_trade_no' => 'scan'.time().'333',
            'total_amount' => '100',
            'subject' => 'yansongda 测试 - 01',
        ]);
        return $result->qr_code;
        // return $result;
    }


    /**
     * 转账
     */
    public function transfer()
    {
        Pay::config($this->config);
        $result = Pay::alipay()->transfer([
            'out_biz_no' => '202106051431'.time(),
            'trans_amount' => '200',
            'product_code' => 'TRANS_ACCOUNT_NO_PWD',
            'biz_scene' => 'DIRECT_TRANSFER',
            'payee_info' => [
                'identity' => 'uwlqwu7588@sandbox.com',
                // 'identity' => 'ghdhjw7124@sandbox.com',
                'identity_type' => 'ALIPAY_LOGON_ID',
                'name' => 'uwlqwu7588'
            ],
        ]);
        return $result;
    }




    public function returnCallback()
    {
        $data = Pay::alipay($this->config)->callback(); // 是的，验签就这么简单！

        // 订单号：$data->out_trade_no
        // 支付宝交易号：$data->trade_no
        // 订单总金额：$data->total_amount
    }

    public function notifyCallback()
    {
        $alipay = Pay::alipay($this->config);

        try{
            $data = $alipay->callback(); // 是的，验签就这么简单！

            // 请自行对 trade_status 进行判断及其它逻辑进行判断，在支付宝的业务通知中，只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时，支付宝才会认定为买家付款成功。
            // 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号；
            // 2、判断total_amount是否确实为该订单的实际金额（即商户订单创建时的金额）；
            // 3、校验通知中的seller_id（或者seller_email) 是否为out_trade_no这笔单据的对应的操作方（有的时候，一个商户可能有多个seller_id/seller_email）；
            // 4、验证app_id是否为该商户本身。
            // 5、其它业务逻辑情况
        } catch (\Exception $e) {
            // $e->getMessage();
        }

        return $alipay->success();
    }

}